package com.hengda.museumonline.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.hengda.museumonline.MyApplication;
import com.hengda.museumonline.R;
import com.hengda.museumonline.entity.MuseumBean;
import com.hengda.museumonline.http.CallServer;
import com.litesuits.common.utils.PackageUtil;
import com.yolanda.nohttp.NoHttp;
import com.yolanda.nohttp.RequestMethod;
import com.yolanda.nohttp.rest.OnResponseListener;
import com.yolanda.nohttp.rest.Request;
import com.yolanda.nohttp.rest.Response;

import static com.yolanda.nohttp.NoHttp.getContext;

/**
 * @author：JFWU on 2016/10/19 18:32
 * @E-mail：jeffy12138@126.com
 */
public class MuseumContentActivity extends BaseActivity {
    private int id;
    private MuseumBean museumBean;
    private android.widget.ImageView itembackground;
    private android.widget.TextView title;
    private android.widget.TextView time;
    private android.widget.TextView source;
    private android.widget.TextView name;
    private android.widget.TextView itemlocation;
    private android.widget.TextView itemphone;
    private android.widget.TextView itemprice;
    private android.widget.TextView itemstarttime;
    private android.widget.TextView excerpt;
    private android.widget.LinearLayout picurl;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_museumcontent);
        this.picurl = (LinearLayout) findViewById(R.id.pic_url);
        this.excerpt = (TextView) findViewById(R.id.excerpt);
        this.itemstarttime = (TextView) findViewById(R.id.item_start_time);
        this.itemprice = (TextView) findViewById(R.id.item_price);
        this.itemphone = (TextView) findViewById(R.id.item_phone);
        this.itemlocation = (TextView) findViewById(R.id.item_location);
        this.name = (TextView) findViewById(R.id.name);
        this.source = (TextView) findViewById(R.id.source);
        this.time = (TextView) findViewById(R.id.time);
        this.title = (TextView) findViewById(R.id.title);
        this.itembackground = (ImageView) findViewById(R.id.item_background);
        id = Integer.parseInt(getIntent().getStringExtra("id"));
        getContentfromNet();
        System.out.println("id:" + id);
    }

    private void getContentfromNet() {
        Request request = NoHttp.createStringRequest(MyApplication.url + "showroom/content", RequestMethod.GET);
        request.add("p", "a");
        request.add("v", PackageUtil.getAppPackageInfo(this).versionName);
        request.add("id", id);

        CallServer.getRequestInstance().add(this, 0, request, new OnResponseListener() {
            @Override
            public void onStart(int what) {
            }

            @Override
            public void onSucceed(int what, Response response) {
                System.out.println("response:" + response.get().toString());
                if (200 == response.getHeaders().getResponseCode()) {
                    Gson gson = new Gson();
                    museumBean = gson.fromJson(response.get().toString(), MuseumBean.class);
                    if (museumBean != null && museumBean.getData() != null) {
                        setData(museumBean);
                    }
                }
            }

            @Override
            public void onFailed(int what, String url, Object tag, Exception exception, int responseCode, long networkMillis) {
                System.out.println("onFailed:" + exception);
            }

            @Override
            public void onFinish(int what) {
            }
        });
    }

    private void setData(MuseumBean museumBean) {
        Glide.with(getContext()).load(museumBean.getData().getItem_background()).into(itembackground);
        title.setText(museumBean.getData().getTitle());
        time.setText(museumBean.getData().getStart_time() + "-" + museumBean.getData().getEnd_time());
        source.setText(museumBean.getData().getSource());
        name.setText(museumBean.getData().getName());
        itemlocation.setText(museumBean.getData().getItem_location());
        itemphone.setText(museumBean.getData().getItem_phone());
        itemprice.setText(museumBean.getData().getItem_price());
        itemstarttime.setText(museumBean.getData().getItem_start_time());
        excerpt.setText(museumBean.getData().getExcerpt());
        if ((museumBean.getData().getPic_url() != null && museumBean.getData().getPic_url().size() > 0)) {
            for (String s : museumBean.getData().getPic_url()) {
                ImageView imageView = new ImageView(getContext());
                imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                Glide.with(getContext()).load(s).into(imageView);
                picurl.addView(imageView);
            }
        }

    }
}
